package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.annotation.RequiresRoles;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.pojo.Brand;
import com.spzx.product.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-03-12
 */
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Autowired
    private IBrandService brandService;

    /**
     * 获取品牌列表
     *
     * @param name 品牌名称
     * @return getDataTable(list) 品牌列表
     */
    @Operation(summary = "获取品牌列表")
    // @RequiresLogin  // 设置访问权限：登入
    // @RequiresRoles("admin")  // 设置访问权限：用户角色
    @RequiresPermissions("product:brand:list")  // 设置访问权限：权限字符
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(name = "name", description = "品牌名称") String name) {
        // 分页
        startPage();

        List<Brand> list = brandService.selectBrandList(name);
        return getDataTable(list);
    }

    /**
     * 根据品牌编号获取详细信息
     */
    @Operation(summary = "根据品牌编号获取详细信息")
    @RequiresPermissions("product:brand:query")
    @Log(title = "品牌管理", businessType = BusinessType.QUERY)
    @GetMapping(value = "/{brandId}")
    public AjaxResult getInfo(@Parameter(name = "brandId", description = "品牌编号") @PathVariable Long brandId) {
        return success(brandService.getById(brandId));
    }

    /**
     * 新增品牌
     */
    @Operation(summary = "新增品牌")
    @RequiresPermissions("product:brand:add")
    @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(/*@Validated*/ @RequestBody Brand brand) {
        if (!brandService.checkBrandNameUnique(brand)) {
            return error("新增品牌'" + brand.getName() + "'失败，该品牌名称已存在");
        }
        brand.setCreateBy(SecurityUtils.getUsername());
        return toAjax(brandService.save(brand));
    }

    /**
     * 修改品牌
     */
    @Operation(summary = "修改品牌")
    @RequiresPermissions("product:brand:edit")
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(/*@Validated*/ @RequestBody Brand brand) {
        if (!brandService.checkBrandNameUnique(brand)) {
            return error("修改岗位'" + brand.getName() + "'失败，岗位名称不存在");
        }
        brand.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(brandService.updateById(brand));
    }

    /**
     * 删除品牌
     */
    @Operation(summary = "根据品牌编号删除品牌")
    @RequiresPermissions("product:brand:remove")
    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{brandIds}")
    public AjaxResult remove(@Parameter(name = "brandIds", description = "品牌编号列表") @PathVariable List<Long> brandIds) {
        return toAjax(brandService.removeBatchByIds(brandIds));
    }

    @Operation(summary = "获取所有品牌")
    // @RequiresLogin  // 设置访问权限：登入
    // @RequiresRoles("admin")  // 设置访问权限：用户角色
    // @RequiresPermissions("product:brand:getBrandAll")  // 设置访问权限：权限字符
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll() {
        List<Brand> list = brandService.list();
        return success(list);
    }

    @InnerAuth
    @Operation(summary = "测试")
    @GetMapping("/test")
    public AjaxResult test() {
        List<Brand> list = brandService.list();
        return success(list);
    }
}
